import { BaseAvatar } from "./BaseAvatar";
import { SEntityInfo } from "../../../../Message/BroadCast/SEntityInfo";
import { Cache } from "../../cache/Cache";
import { Dispatcher } from "../../../mvc/core/Dispatcher";
import { EventName } from "../../mvc/EventName";
import { AvatarEvent } from "./AvatarEvent";
import { SPetInfo } from "../../../../Message/Game/SPetInfo";
import { PetUtil } from "../pet/info/PetUtil";
import { ThingUtil } from "../../scene/layer/utils/ThingUtil";
import { MsgManager } from "../../manager/MsgManager";
import { Language } from "../../../../extend/language/Language";
import { DataEvent } from "../../events/DataEvent";
import { SPetStatusUpdate } from "../../../../Message/Game/SPetStatusUpdate";
import { EPetStatus } from "../../../../Message/Game/EPetStatus";
import { PetCDItem } from "../pet/PetCDItem";
import { PetCoolDownInfo } from "../pet/PetCoolDownInfo";
import { ShortcutsKey } from "../../../common/shortcutsKey/ShortcutsKey";
import { ModuleType } from "../common/ModuleType";
import { HTMLUtil } from "../../../../com/gengine/utils/HTMLUtil";
import { BaseProgressBar } from "../common/BaseProgressBar";
import { ImagesConst } from "../../resource/ImagesConst";
import { BuffInfo } from "./BuffInfo";
import { SkillStateConfig } from "../../resource/SkillStateConfig";
import { ImageInfo } from "../../../../com/gengine/resource/info/ImageInfo";
import { PetInfo } from "../../resource/info/PetInfo";
import { GButton } from "../../../../com/mui/controls/GButton";
import { ObjCreate } from "../common/ObjCreate";
import { PetEventName } from "../pet/PetEventName";
import { FeedType } from "../pet/FeedType";
import { GlobalStyle } from "../../../common/global/GlobalStyle";
import { Energy } from "../../../../FeedType/Energy";
type int = number;
//class PetAvatar
    
    export  class PetAvatar extends BaseAvatar
    {
        constructor(arg1: SEntityInfo)
        {
            super(arg1);
            this.selfSEntityInfo = arg1;
            this.petInfo = Cache.instance.pet.battlePet;
            Dispatcher.addEventListener(EventName.SysSettingSuccess, this.resetSlider);
            this.addEventListener(AvatarEvent.ChangeLifeSet, this.onLifeSetHandler);
            Dispatcher.addEventListener(EventName.PetStatusUpdate, this.onPetStatusUpdateHandler);
            Dispatcher.addEventListener(EventName.PetBattleSuccess, this.onPetBattleSuccessHandler);
            this.mouseEnabled = false;
            this.startCoolDown(0, this.petInfo.uid);
            this.updateShortcutKey();
            return;
        }

        public set petInfo(arg1: SPetInfo)
        {
            this._petInfo = arg1;
            if (this._petInfo) 
            {
                this.updateEnergy(int(this._petInfo.energy / 3), int(this._petInfo.maxEnergy / 3));
            }
            return;
        }

        /* internal  */onOperMenuSpriteClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=null;
            if (PetUtil.isBattled(this._petInfo)) 
            {
                loc1 = ThingUtil.entityUtil.getEntity(this.selfSEntityInfo.entityId);
                if (loc1) 
                {
                    loc1.selected = true;
                }
            }
            else 
            {
                MsgManager.showRollTipsMsg(Language.getString(54630));
            }
            return;
        }

        /* internal  */onPetStatusUpdateHandler(arg1: DataEvent): void
        {
            var loc3=null;
            var loc1=arg1.data as SPetStatusUpdate;
            var loc2=loc1.status.__value;
            if (loc1.uid == this._petInfo.uid || loc2 == EPetStatus._EPetActive || loc2 == EPetStatus._EPetPassive || loc2 == EPetStatus._EPetDefense) 
            {
                this.updateModeBtnByStatus(loc2);
            }
            return;
        }

        /* internal  */updateModeBtnByStatus(arg1: int): void
        {
            if (arg1 >= 1 && arg1 <= 3) 
            {
                this._modeBtn.toolTipData = this.getTipByStatus(arg1);
                this._modeBtn.styleName = this.getModeBtnStyleByStatus(arg1);
            }
            return;
        }

        /* internal  */getTipByStatus(arg1: int): string
        {
            var loc1=null;
            if (arg1 != EPetStatus._EPetActive) 
            {
                if (arg1 != EPetStatus._EPetPassive) 
                {
                    if (arg1 == EPetStatus._EPetDefense) 
                    {
                        loc1 = Language.getString(54439);
                    }
                }
                else 
                {
                    loc1 = Language.getString(54438);
                }
            }
            else 
            {
                loc1 = Language.getString(54437);
            }
            return loc1;
        }

        public /* override */ init(): void
        {
            this.isShowHealthText = false;
            this.isShowManaText = false;
            super.init();
            this.createSBtn();
            this.createCBtn();
            this.createHBtn();
            this.createModeBtn();
            createEffects(4, 76, 37);
            this.createEnergyBar();
            createLifeSlider(AvatarEvent.PetLifeSliderName);
            createManaSlider(AvatarEvent.PetManaSliderName);
            Dispatcher.dispatchEvent(new DataEvent(EventName.SysSettingSuccess, null));
            this.updateAvatar(entityInfo.code);
            this.resetAvatar();
            Dispatcher.addEventListener(EventName.HeaderSliderUp, this.sliderUp);
            manaSlider.visible = false;
            magicBar.setValue(100, 100);
            magicBar.removeEventListener(flash.events.MouseEvent.ROLL_OVER, onManaBarRollOver);
            magicBar.removeEventListener(flash.events.MouseEvent.ROLL_OUT, onBarRollOut);
            magicBar.removeEventListener(flash.events.MouseEvent.MOUSE_MOVE, onBarMouseMove);
            magicBar.removeEventListener(flash.events.MouseEvent.CLICK, onMagicBarMouseClick);
            this._operMenuSprite = new Object /* flash.display.Sprite */();
            this._operMenuSprite.buttonMode = true;
            this._operMenuSprite.graphics.beginFill(16711680, 0);
            this._operMenuSprite.graphics.drawCircle(34, 25, 20);
            this._operMenuSprite.cacheAsBitmap = true;
            this.addChild(this._operMenuSprite);
            this._operMenuSprite.addEventListener(flash.events.MouseEvent.CLICK, this.onOperMenuSpriteClickHandler);
            this._cdItem = new PetCDItem();
            this._cdItem.x = 33;
            this._cdItem.y = 28;
            this.addChild(this._cdItem);
            return;
        }

        /* internal  */getModeBtnStyleByStatus(arg1: int): string
        {
            var loc1=null;
            if (arg1 != EPetStatus._EPetActive) 
            {
                if (arg1 != EPetStatus._EPetPassive) 
                {
                    if (arg1 == EPetStatus._EPetDefense) 
                    {
                        loc1 = "FollowBtn";
                    }
                }
                else 
                {
                    loc1 = "DefenseBtn";
                }
            }
            else 
            {
                loc1 = "ActiveBtn";
            }
            return loc1;
        }

        /* internal  */onPetBattleSuccessHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data["uid"] as string;
            this.startCoolDown(0, loc1);
            return;
        }

        /* internal  */onPetRestSuccessHandler(arg1: DataEvent): void
        {
            return;
        }

        /* internal  */onPetDeathHandler(): void
        {
            this.startCoolDown(3, "PetRevivalCD", true);
            return;
        }

        /* internal  */startCoolDown(arg1: int, arg2: string, arg3: Boolean=false): void
        {
            var loc1=null;
            var loc2=null;
            if (this._petInfo) 
            {
                loc2 = new PetCoolDownInfo(arg2, arg1);
                if (this._cdItem.playingEffect) 
                {
                    this._cdItem.clearEffect();
                }
                (loc1 = Cache.instance.timeCdCache.addPetCd(loc2)).startCoolDown();
                this._cdItem.isShowCdLeftTime = arg3;
                this._cdItem.recallCdTime = loc1;
            }
            return;
        }

        public updateShortcutKey(): void
        {
            var loc2=null;
            var loc3=null;
            var loc1=ShortcutsKey.instance.getKeyMap(ModuleType.PetsRest);
            if (loc1) 
            {
                if (loc1.keyData) 
                {
                    loc3 = loc1.keyData.shortcutsName;
                }
                if (loc3 == "") 
                {
                    this._sBtn.toolTipData = HTMLUtil.addColor(Language.getStringByParam(50034, ""), "#FFFFFF");
                    this._cBtn.toolTipData = HTMLUtil.addColor(Language.getStringByParam(50035, ""), "#FFFFFF");
                }
                else 
                {
                    this._sBtn.toolTipData = HTMLUtil.addColor(Language.getStringByParam(50034, "（" + loc3 + "）"), "#FFFFFF");
                    this._cBtn.toolTipData = HTMLUtil.addColor(Language.getStringByParam(50035, "（" + loc3 + "）"), "#FFFFFF");
                }
            }
            return;
        }

        public createEnergyBar(): void
        {
            this._energyBar = new BaseProgressBar();
            this._energyBar.isScale = true;
            this._energyBar.setSize(79, 8);
            this._energyBar.showProgressText = false;
            this._energyBar.setLabelPosition(0, -4);
            this._energyBar.progressBarClass = ImagesConst.EnergyBar;
            this._energyBar.setValue(int(this.petInfo.energy / 3), int(this.petInfo.maxEnergy / 3));
            this._energyBar.addEventListener(flash.events.MouseEvent.ROLL_OUT, this.onEnergyBarRollOut);
            this._energyBar.addEventListener(flash.events.MouseEvent.ROLL_OVER, this.onEnergyBarRollOver);
            this._energyBar.addEventListener(flash.events.MouseEvent.MOUSE_MOVE, this.onEnergyBarMouseMove);
            this._energyBar.x = 60;
            this._energyBar.y = 25;
            this.addChild(this._energyBar);
            return;
        }

        public resetAvatar(): void
        {
            mainBgName = ImagesConst.PetBg;
            healthBar.setSize(79, 8);
            healthBar.progressBarClass = ImagesConst.Shengmingtiao;
            magicBar.setSize(79, 8);
            magicBar.progressBarClass = ImagesConst.Mofatiao;
            lifeSlider.thubmName = "ControlBarSmall";
            manaSlider.thubmName = "ControlBarSmall";
            this.resetPosition();
            nameLabel.width = 65;
            this.removeChild(magicBar);
            this.removeChild(manaSlider);
            return;
        }

        public /* override */ updateAvatarUI(arg1: SEntityInfo): void
        {
            var loc1=null;
            this.selfSEntityInfo = arg1;
            if (Cache.instance.pet.battlePet) 
            {
                loc1 = Cache.instance.pet.battlePet;
                if (!(loc1.energy == this.petInfo.energy) || !(loc1.maxEnergy == this.petInfo.maxEnergy)) 
                {
                    this.updateEnergy(int(this.petInfo.energy / 3), int(this.petInfo.maxEnergy / 3));
                }
                this.petInfo = loc1;
            }
            else 
            {
                this.updatePetInfo(arg1);
            }
            if (arg1.life <= 0) 
            {
                this.onPetDeathHandler();
            }
            super.updateAvatarUI(arg1);
            this.updateAvatar(arg1.code);
            magicBar.setValue(100, 100);
            return;
        }

        public /* override */ updateBufferByTSateIdArray(arg1: Array<any>): void
        {
            var loc2=null;
            var loc3=0;
            var loc1=[];
            var loc4=0;
            while (loc4 < arg1.length) 
            {
                loc3 = arg1[loc4];
                loc2 = new BuffInfo();
                loc2.tstate = SkillStateConfig.instance.getInfoByName(loc3);
                if (!Cache.instance.buff.isWuXingBuff(loc2)) 
                {
                    loc1.push(loc2);
                }
                ++loc4;
            }
            super.updateBufferByBuffInfoArray(loc1);
            return;
        }

        /* internal  */updatePetInfo(arg1: SEntityInfo): void
        {
            if (this.petInfo && arg1) 
            {
                this.petInfo.name = arg1.name;
                this.petInfo.level = arg1.level;
                this.petInfo.life = arg1.life;
                this.petInfo.fightAttribute.maxLife = arg1.maxLife;
                this.petInfo.mana = arg1.mana;
                this.petInfo.fightAttribute.maxMana = arg1.maxMana;
            }
            return;
        }

        protected /* override */ onHealthBarRollOver(arg1: Object /* flash.events.MouseEvent */): void
        {
            super.onHealthBarRollOver(arg1);
            labelTip.data = this.getTipData();
            return;
        }

        protected /* override */ onAvatarLoaded(arg1: ImageInfo): void
        {
            avatarBitmap.bitmapData = arg1.bitmapData;
            avatarBitmap.width = 42;
            avatarBitmap.height = 42;
            return;
        }

        /* internal  */updateAvatar(arg1: int): void
        {
            avatarName = PetInfo.getPetAvatarName(arg1);
            return;
        }

        public resetPosition(): void
        {
            levelLabel.x = 11;
            levelLabel.y = 35;
            nameLabel.x = 62;
            nameLabel.y = -3;
            healthBar.move(60, 16);
            magicBar.move(60, 25);
            avatarBitmap.x = 13;
            avatarBitmap.y = 6;
            lifeSlider.width = 76;
            lifeSlider.x = 60;
            lifeSlider.y = 14;
            return;
        }

        public createSBtn(): void
        {
            this._sBtn = new GButton();
            this._sBtn.label = "";
            this._sBtn.setSize(25, 23);
            this._sBtn.styleName = "SPetButton";
            this._sBtn.x = 5;
            this._sBtn.y = 49;
            this._sBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onRestClickHandler);
            this.addChild(this._sBtn);
            return;
        }

        public createCBtn(): void
        {
            this._cBtn = new GButton();
            this._cBtn.label = "";
            this._cBtn.setSize(25, 23);
            this._cBtn.styleName = "CPetButton";
            this._cBtn.x = 5;
            this._cBtn.y = 49;
            this._cBtn.visible = false;
            this._cBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onBattleClickHandler);
            this.addChild(this._cBtn);
            return;
        }

        public createHBtn(): void
        {
            this._hBtn = new GButton();
            this._hBtn.label = "";
            this._hBtn.setSize(25, 23);
            this._hBtn.styleName = "HPetButton";
            this._hBtn.x = 26;
            this._hBtn.y = 49;
            this._hBtn.toolTipData = HTMLUtil.addColor(Language.getString(50036), "#FFFFFF");
            this._hBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onHBtnClickHandler);
            this.addChild(this._hBtn);
            return;
        }

        public createGBtn(): void
        {
            this._gBtn = new GButton();
            this._gBtn.label = "";
            this._gBtn.setSize(25, 23);
            this._gBtn.styleName = "GPetButton";
            this._gBtn.x = 76;
            this._gBtn.y = 49;
            this.addChild(this._gBtn);
            return;
        }

        public createModeBtn(): void
        {
            this._modeBtn = ObjCreate.createGButton("", 47, 49, 25, 23, this);
            this._modeBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onModeBtnClickHandler);
            this.updateModeBtnByStatus(EPetStatus._EPetPassive);
            return;
        }

        public onModeBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=this.getNextStatus();
            Dispatcher.dispatchEvent(new DataEvent(EventName.PetStatusChange, {"uid": this._petInfo.uid, "status": loc1}));
            return;
        }

        /* internal  */getNextStatus(): int
        {
            var loc1=0;
            var loc2=this._petInfo.status;
            loc1 = loc2 + 1;
            if (loc1 >= 4) 
            {
                loc1 = EPetStatus._EPetActive;
            }
            return loc1;
        }

        public showCBtn(arg1: Boolean=true): void
        {
            if (arg1) 
            {
                this._cBtn.visible = true;
                this._sBtn.visible = false;
                this._modeBtn.visible = false;
                buffPanel.visible = false;
            }
            else 
            {
                this._cBtn.visible = false;
                this._sBtn.visible = true;
                this._modeBtn.visible = true;
                buffPanel.visible = true;
            }
            return;
        }

        /* internal  */onRestClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.PetRest));
            return;
        }

        /* internal  */onBattleClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.PetBattle, this.petInfo));
            return;
        }

        /* internal  */onLifeSetHandler(arg1: DataEvent): void
        {
            var loc1=(arg1.data as int) / 100;
            var loc2=manaSlider.value / 100;
            Dispatcher.dispatchEvent(new DataEvent(EventName.HeaderSliderChange, {"id":"pet", "lifeSet": loc1, "manaSet": loc2}));
            return;
        }

        /* internal  */onManaSetHandler(arg1: DataEvent): void
        {
            var loc1=lifeSlider.value / 100;
            var loc2=(arg1.data as int) / 100;
            Dispatcher.dispatchEvent(new DataEvent(EventName.HeaderSliderChange, {"id":"pet", "lifeSet": loc1, "manaSet": loc2}));
            return;
        }

        /* internal  */sliderUp(arg1: DataEvent): void
        {
            var loc1=arg1.data as string;
            var loc2=this.lifeSlider.value / 100;
            var loc3=this.manaSlider.value / 100;
            if (loc1 != AvatarEvent.PetLifeSliderName) 
            {
                if (loc1 == AvatarEvent.PetManaSliderName) 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.HeaderSliderChange, {"id":"pet", "lifeSet": loc2, "manaSet": loc3}));
                }
            }
            else 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.HeaderSliderChange, {"id":"pet", "lifeSet": loc2, "manaSet": loc3}));
            }
            return;
        }

        public resetSlider(arg1: DataEvent): void
        {
            this.manaSlider.value = Cache.instance.autoResumeSet.petManaSet * 100;
            this.lifeSlider.value = Cache.instance.autoResumeSet.petLifeSet * 100;
            this.manaSlider.thumbData = getManaSetToolTip(manaSlider.value);
            this.lifeSlider.thumbData = getLifeSetToolTip(lifeSlider.value);
            Dispatcher.dispatchEvent(new DataEvent(EventName.PetAutoResume));
            return;
        }

        public onHBtnClickHandler(arg1: Object /* flash.events.Event */): void
        {
            Dispatcher.dispatchEvent(new DataEvent(PetEventName.PetFeedClick, {"petInfo": this.petInfo, "feedType": Energy}));
            return;
        }

        /* internal  */onEnergyBarRollOver(arg1: Object /* flash.events.MouseEvent */): void
        {
            labelTip.data = this.getTipData();
            labelTip.visible = true;
            return;
        }

        /* internal  */onEnergyBarRollOut(arg1: Object /* flash.events.MouseEvent */): void
        {
            labelTip.visible = false;
            return;
        }

        /* internal  */onEnergyBarMouseMove(arg1: Object /* flash.events.MouseEvent */): void
        {
            labelTip.x = arg1.stageX + 15;
            labelTip.y = arg1.stageY + 15;
            return;
        }

        /* internal  */getTipData(): string
        {
            return HTMLUtil.addColor(Language.getString(50037), GlobalStyle.color7) + HTMLUtil.addColor(entityInfo.life + "<font color=\'#ff0000\' size=\'16\'><b> / </b></font>" + entityInfo.maxLife, "#ffffff") + "\n" + HTMLUtil.addColor(Language.getString(50038), GlobalStyle.color6) + HTMLUtil.addColor(int(this.petInfo.energy / 3) + "<font color=\'#f0ea3f\' size=\'16\'><b> / </b></font>" + int(this.petInfo.maxEnergy / 3), "#ffffff");
        }

        public updateEnergy(arg1: int, arg2: int): void
        {
            if (this._energyBar) 
            {
                this._energyBar.setValue(arg1, arg2);
            }
            return;
        }

        public get petInfo(): SPetInfo
        {
            return this._petInfo;
        }

        /* internal */ /* var */_operMenuSprite: Object /* flash.display.Sprite */;

        /* internal */ /* var */_sBtn: GButton;

        /* internal */ /* var */_cBtn: GButton;

        /* internal */ /* var */_hBtn: GButton;

        /* internal */ /* var */_gBtn: GButton;

        /* internal */ /* var */_energyBar: BaseProgressBar;

        /* internal */ /* var */_cdItem: PetCDItem;

        /* internal */ /* var */_backupCode: int;

        /* internal */ /* var */_petInfo: SPetInfo;

        public /* var */selfSEntityInfo: SEntityInfo;

        /* internal */ /* var */_modeBtn: GButton;
    }
